const INTRO_MAP = new Map();
const _INDEX = 7;
const _IST = '720';
const _UNIST = '520';
const TIPS_MAP = new Map();

$.getJSON('js/introList.json', function (data) {
    //console.log('introList', data)
    // 遍历父词条
    for (let i = 0; i < data.length; i++) {
        let row = data[i]
        let hasChildWord = false;

        // 找出符合爸爸=我=父词条 的页面节点并遍历，没有属于父词条的页面节点，那就直接跳过。
        let nodes = $("td:contains("+row.spln+")");
        if (nodes.length === 0) continue;

        nodes.each(function () {
            // 遍历爸爸的儿子们
            for (let k = 0; k < row.cs.length; k++) {
                //某个儿子
                let tmpC = row.cs[k];
                // 判断符合爸爸的页面节点 是否也符合这个儿子。如果是，将这个儿子的链接信息插入到这个页面节点
                if ($(this).text().indexOf(tmpC.spln) > 0) {
                    hasChildWord = true;
                    // count--;
                    let html = $(this).html()
                    html = html.replaceAll(tmpC.spln, "<a class='popable' onclick='popMenu(\"" + tmpC.spln + "\");' >"+tmpC.spln+"<i class='iconHighlight'></i></a>")
                    $(this).html(html);
                    INTRO_MAP.set(tmpC.spln, tmpC);
                }
            }

            let reg = new RegExp("(?="+row.spln+").*?(?=<)","g");
            let html = $(this).html();
            html = html.replaceAll(reg, "<a class='popable' onclick='popMenu(\"" + row.spln + "\");' >"+row.spln+"<i class='iconHighlight'></i></a>")
            $(this).html(html);
            INTRO_MAP.set(row.spln, Object.assign({}, row));
            INTRO_MAP.get(row.spln).cs = []; //去除cs

        })
    }

})
$.getJSON('js/tips.json', function (data) {

    //console.log('tips', data)
    // 遍历父词条
    for (let i = 0; i < data.length; i++) {
        let row = data[i]
        let hasChildWord = false;

        let nodes = $(".para:contains("+row.name+")");
        if (nodes.length === 0) continue;
        console.log('div:contains', nodes)
        nodes.each(function () {
        if ($(this).text().indexOf(row.name) > 0) {
                console.log('replace', this)
                    let reg = new RegExp("[^>&quot;]"+row.name,"g");
                    let html = $(this).html()
                    html = html.replaceAll(reg, "<a class='tipsable' onclick='popTips(\"" + row.name + "\");' >"+row.name+"<i class='iconHighlight'></i></a>")
                    $(this).html(html);
                    TIPS_MAP.set(row.name, row.content);
        }
        })
    }

})

var x = ""
var y = ""
$("body").mousedown(function (e) {
	if (1 == e.which) {
		//获取左键键点击坐标
		x = e.clientX ;
		y = e.clientY ;
        if (x<200){
            x = e.clientX;
        }else{
            x = e.clientX-200 ;
        }
        if (y<200){
            y = e.clientY+20 ;
        }else{
            y = e.clientY-220 ;
        }
	}
    if(x==""||x==null||x==undefined){
		x = e.clientX - 55;
	}
	if(y==""||y==null||y==undefined){
		y = e.clientY - 55;
	}

})

function popMenu (key) {

    let intro = INTRO_MAP.get(key);

    let codes = intro.desc.split("-");


    intro.ist = '';
    intro.unist = '';


    for (let i = 0; i < codes.length; i++) {
        if (i !== _INDEX) {
            intro.ist += codes[i];
            intro.unist += codes[i];
        } else {
            // '720' + 'A'
            intro.ist += _IST + codes[i].charAt(codes[i].length-1);
            intro.unist += _UNIST + codes[i].charAt(codes[i].length-1);
        }
        if (i < codes.length - 1) {
            intro.ist += '-';
            intro.unist += '-';
        }
    }


    $(".popMenu").remove()
    let menuDiv = window.document.createElement('div')
    menuDiv.id = 'popMenu'
    menuDiv.classList.add('popMenu')

    let tittleDIV = window.document.createElement('div')
    tittleDIV.id = 'popMenuTittle'
    tittleDIV.classList.add('menuDivCss')
    let pText = window.document.createElement('p')
    pText.innerText = '描述'
    pText.classList.add('pTextCss')
    tittleDIV.appendChild(pText)
    menuDiv.appendChild(tittleDIV)

    let table = window.document.createElement('table')

    let closeBtn = window.document.createElement('img')
    closeBtn.src='img/whiteDelete.png'
    // closeBtn.innerText= '[X]'
    closeBtn.classList.add('close')
    table.appendChild(closeBtn)
    closeBtn.onclick = function () {
        $(".popMenu").remove()
    }

    //tr1
    let tr1 = window.document.createElement('tr')
    let td11 = window.document.createElement('td')
    td11.innerText = '描述'
    td11.classList.add('labeler')
    let td12 = window.document.createElement('td')
    let a = window.document.createElement('a')
    a.innerText = intro.spln
    a.onclick = function () {
        dmRefLink(intro.desc)
    }
    td12.appendChild(a)
    tr1.appendChild(td11)
    tr1.appendChild(td12)
    table.appendChild(tr1)


    //tr2
    let tr2 = window.document.createElement('tr')
    let td21 = window.document.createElement('td')
    td21.innerText = '拆卸/安装'
    td21.classList.add('labeler')
    let td22 = window.document.createElement('td')
    let a221 = window.document.createElement('a')
    a221.innerText = '安装'
    a221.onclick = function () {
        dmRefLink(intro.ist)
    }
    let a222 = window.document.createElement('a')
    a222.innerText = '拆卸'
    a222.onclick = function () {
        dmRefLink(intro.unist)
    }
    td22.appendChild(a221)
    //td22.innerHTML += '/'
    td22.appendChild(a222)
    tr2.appendChild(td21)
    tr2.appendChild(td22)
    table.appendChild(tr2)

    let tr3 = window.document.createElement('tr')
    let td31 = window.document.createElement('td')
    td31.innerText = '相关'
    td31.classList.add('labeler')
    let td32 = window.document.createElement('div')
    td32.innerText = '搜索'
    td32.classList.add('search')
    td32.onclick = function () {
        $(".popMenu").remove()
        // alert(parent.$("#key").val())
        window.parent.$("#key").val(intro.cpln)
        window.parent.$('#search').click()
    }

    tr3.appendChild(td31)
    tr3.appendChild(td32)
    table.appendChild(tr3)


    menuDiv.appendChild(table)

    window.document.body.appendChild(menuDiv)

    $("#popMenu").css("left",x);
    $("#popMenu").css("top",y);
}
function popTips (key) {
    let tips = TIPS_MAP.get(key);

    $(".tips").remove();

    let tipsDiv = window.document.createElement('div');
    tipsDiv.id = 'tips';
    tipsDiv.classList.add('tips');
    tipsDiv.innerText = tips
    let closeBtn = window.document.createElement('img')
    closeBtn.src='img/close.png'
    closeBtn.classList.add('close')
    tipsDiv.appendChild(closeBtn)
    closeBtn.onclick = function () {
        $(".tips").remove()
    }
    window.document.body.appendChild(tipsDiv)

    $("#tips").css("left",x);
    $("#tips").css("top",y);


}
